﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml;
using WikiQuizz.Common.WikiAPIHelper;
using System.Xml.Linq;
using System.ComponentModel;
using System.Windows.Input;
using WikiQuizz.Common.Entities;
using WikiQuizz.Common.Interfaces;
using WikiQuizz.StandardImplementations;
using System.Collections.Specialized;
using System.Windows.Data;
using WikiQuizz.Common.Resource;
using System.Threading;
using System.Windows.Forms;
using WikiQuizz.Common.Utils;
using System.Windows;
using System.Diagnostics;

namespace WikiQuizz.ViewModel
{
    public class MainWindowViewModel : INotifyPropertyChanged 
    {

        IGameManager _GameManager;

        public MainWindowViewModel()
        {
            _GameManager = new GameManager();
            ExecuteStartNewGame(null);
            
            BindCommands();            
        }

        #region properties
        public event PropertyChangedEventHandler PropertyChanged;

        private int _CorrectAnswers;
        public int CorrectAnswers
        {
            get
            {
                return _CorrectAnswers;
            }

            set
            {
                if (_CorrectAnswers != value)
                {
                    _CorrectAnswers = value;
                    NotifyPropertyChanged("CorrectAnswers");
                }
            }
        }

        private int _InCorrectAnswers;
        public int InCorrectAnswers
        {
            get
            {
                return _InCorrectAnswers;
            }

            set
            {
                if (_InCorrectAnswers != value)
                {
                    _InCorrectAnswers = value;
                    NotifyPropertyChanged("InCorrectAnswers");
                }
            }
        }


        private string _Question;
        public string Question
        {
            get
            {
                return _Question;
            }

            set
            {
                if (_Question != value)
                {
                    _Question = value;
                    NotifyPropertyChanged("Question");
                }
            }
        }

        private string _AnswerA;
        public string AnswerA
        {
            get
            {
                return _AnswerA;
            }

            set
            {
                if (_AnswerA != value)
                {
                    _AnswerA = value;
                    NotifyPropertyChanged("AnswerA");
                }
            }
        }

        private string _AnswerB;
        public string AnswerB
        {
            get
            {
                return _AnswerB;
            }

            set
            {
                if (_AnswerB != value)
                {
                    _AnswerB = value;
                    NotifyPropertyChanged("AnswerB");
                }
            }
        }

        private string _AnswerC;
        public string AnswerC
        {
            get
            {
                return _AnswerC;
            }

            set
            {
                if (_AnswerC != value)
                {
                    _AnswerC = value;
                    NotifyPropertyChanged("AnswerC");
                }
            }
        }

        private string _AnswerD;
        public string AnswerD
        {
            get
            {
                return _AnswerD;
            }

            set
            {
                if (_AnswerD != value)
                {
                    _AnswerD = value;
                    NotifyPropertyChanged("AnswerD");
                }
            }
        }

        public static string What 
        {
            get
            {
                return Strings.WHAT;
            }
        }

        private bool? _RightAnswer;
        public bool? RightAnswer
        {
            get
            {
                return _RightAnswer;
            }

            set
            {
                if (_RightAnswer != value)
                {
                    _RightAnswer = value;
                    NotifyPropertyChanged("RightAnswer");
                }
            }
        }
        #endregion               
                
        #region Commands
        public ICommand GetExtract { get; set; }

        public ICommand GetCountryData { get; set; }

        public ICommand GetAnswerA { get; set; }
        public ICommand GetAnswerB { get; set; }
        public ICommand GetAnswerC { get; set; }
        public ICommand GetAnswerD { get; set; }
        public ICommand StartBrowser { get; set; }

        public ICommand StartNewGame { get; set; }
        
        private void ExecuteCommand(object param)
        {
            SetAnswers(_GameManager.GetNextQuizzItem());                        
        }        

        private bool CanExecuteCommand(object param)
        {
            return true;
        }                
        #endregion

        #region private Methoden
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
                
        private void BindCommands()
        {
            GetExtract = new DelegateCommand(ExecuteCommand, CanExecuteCommand);
            GetAnswerA = new DelegateCommand(ExecuteAnswerA, CanExecuteCommand);
            GetAnswerB = new DelegateCommand(ExecuteAnswerB, CanExecuteCommand);
            GetAnswerC = new DelegateCommand(ExecuteAnswerC, CanExecuteCommand);
            GetAnswerD = new DelegateCommand(ExecuteAnswerD, CanExecuteCommand);
            StartNewGame = new DelegateCommand(ExecuteStartNewGame, CanExecuteCommand);
            StartBrowser = new DelegateCommand(ExecuteStartBrowser, CanExecuteCommand);
        }

        private void ExecuteStartBrowser(object obj)
        {
            QuizzItemEntity item = _GameManager.GetActualQuizzItem();

            Process.Start(String.Format(Strings.WHAT_URL, item.Country.Title));
        }

        private void ExecuteAnswerA(object obj)
        {
            ExecuteAnswer(AnswerA);            
        }

        private void ExecuteAnswerB(object obj)
        {
            ExecuteAnswer(AnswerB);
        }

        private void ExecuteAnswerC(object obj)
        {
            ExecuteAnswer(AnswerC);
        }

        private void ExecuteAnswerD(object obj)
        {
            ExecuteAnswer(AnswerD);
        }

        private void ExecuteAnswer(string answer)
        {
            QuizzItemEntity item = _GameManager.GetActualQuizzItem();

            switch (item.AnswerCategory.Id)
            {
                case 1:
                    {
                        if (answer == item.Country.Capital)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                        }
                        break;
                    }
                case 2:
                    {
                        if (answer == item.Country.Population)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                            RightAnswer = false;
                        }
                        break;
                    }
                case 3:
                    {
                        if(answer == item.Country.PopulationDensity)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                            RightAnswer = false;
                        }
                        break;
                    }
                case 4:
                    {
                        if(answer == item.Country.OfficialLanguage)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                            RightAnswer = false;
                        }
                        break;
                    }
                case 5:
                    {
                        if(answer == item.Country.Area)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                            RightAnswer = false;
                        }
                        break;
                    }
                case 6:
                    {
                        if (answer == item.Country.ISO3166)
                        {
                            CorrectAnswers++;
                            RightAnswer = true;
                        }
                        else
                        {
                            InCorrectAnswers++;
                            RightAnswer = false;
                        }
                        break;
                    }
                default:
                    break;
            }

            ExecuteCommand(null);
        }

        private void ExecuteStartNewGame(object obj)
        {
            CorrectAnswers = 0;
            InCorrectAnswers = 0;

            _GameManager.InitializeGame();

            SetAnswers(_GameManager.GetActualQuizzItem());
        }

        private void SetAnswers(QuizzItemEntity item)
        {
            ICollection<CountryEntity> AnswerItems = _GameManager.GetShuffledValuesFromActualQuizzItem();

            Question = String.Format(item.AnswerCategory.Sentence, item.Country.Title);
            switch (item.AnswerCategory.Id)
            {
                case 1:
                    {
                        AnswerA = AnswerItems.First().Capital;
                        AnswerB = AnswerItems.Skip(1).First().Capital;
                        AnswerC = AnswerItems.Skip(2).First().Capital;
                        AnswerD = AnswerItems.Skip(3).First().Capital;
                        break;
                    }
                case 2:
                    {
                        AnswerA = AnswerItems.First().Population;
                        AnswerB = AnswerItems.Skip(1).First().Population;
                        AnswerC = AnswerItems.Skip(2).First().Population;
                        AnswerD = AnswerItems.Skip(3).First().Population;
                        break;
                    }
                case 3:
                    {
                        AnswerA = AnswerItems.First().PopulationDensity;
                        AnswerB = AnswerItems.Skip(1).First().PopulationDensity;
                        AnswerC = AnswerItems.Skip(2).First().PopulationDensity;
                        AnswerD = AnswerItems.Skip(3).First().PopulationDensity;
                        break;
                    }
                case 4:
                    {
                        AnswerA = AnswerItems.First().OfficialLanguage;
                        AnswerB = AnswerItems.Skip(1).First().OfficialLanguage;
                        AnswerC = AnswerItems.Skip(2).First().OfficialLanguage;
                        AnswerD = AnswerItems.Skip(3).First().OfficialLanguage;
                        break;
                    }
                case 5:
                    {
                        AnswerA = AnswerItems.First().Area;
                        AnswerB = AnswerItems.Skip(1).First().Area;
                        AnswerC = AnswerItems.Skip(2).First().Area;
                        AnswerD = AnswerItems.Skip(3).First().Area;
                        break;
                    }
                case 6:
                    {
                        AnswerA = AnswerItems.First().ISO3166;
                        AnswerB = AnswerItems.Skip(1).First().ISO3166;
                        AnswerC = AnswerItems.Skip(2).First().ISO3166;
                        AnswerD = AnswerItems.Skip(3).First().ISO3166;
                        break;
                    }
                default:
                    break;
            }            
        }

                                
        #endregion
    }
}
